Why Go?

27 August 2016

Vladislav Mitov

Miracl

Go is...

package main

import "fmt"

func main() {
    fmt.Println("Hello, World")
}

Go is used by...

Guiding principles in the design

Go has simple syntax

package main

import "fmt"

func main() {
    for i := 0; i < 5; i++ {
        fmt.Println(i)
    }

    // while
    sum := 1
    for sum < 1000 {
        sum += sum
        fmt.Println(sum)
    }

    // forever
    // for {
    // }
}

Go doesn't break backwards compatibility

No exception

Protects developers from making mistakes

Fast compilation time

Tool chain

go static analysis tools

Static linking

Cross compilation

Interfaces

Example from our own Miracl cryptography go implementations

type Reader interface {
    Read(p []byte) (n int, err error)
}

func Copy(dst Writer, src Reader) (written int64, err error)
func CopyBuffer(dst Writer, src Reader, buf []byte) (written int64, err error)
func CopyN(dst Writer, src Reader, n int64) (written int64, err error)
func ReadAtLeast(r Reader, buf []byte, min int) (n int, err error)
func ReadFull(r Reader, buf []byte) (n int, err error)

Example from our own Miracl cryptography go implementations

type Rand struct {
    ...
}

// Returns random byte
func (rand *Rand) GetByte() byte {
    ...
}

// Implements io.Reader
func (rand *Rand) Read(p []byte) (n int, err error) {
    for x := 0; x < len(p); x++ {
        p[x] = rand.GetByte()
    }

    return len(p), nil
}

Example from our own Miracl cryptography go implementations

package main

import (
    "encoding/hex"
    "fmt"
    "io"

    "github.com/miracl/gomiracl"
    _ "github.com/miracl/gomiracl/go"
)

func main() {
    impl, _ := miracl.GetImpl(miracl.GO)
    seed, _ := hex.DecodeString("9e8b4178790cd57a5761c4a6f164ba72")
    rng := miracl.NewSeededRand(impl, seed)

    randomNum := make([]byte, 12)
    io.ReadFull(rng, randomNum)

    fmt.Println(randomNum)
}

Concurrency

Simple Producer-Consumer example

package main

import "fmt"

var msgs = make(chan int)
var done = make(chan bool)

func produce() {
    for i := 0; i < 10; i++ {
        msgs <- i
    }
    done <- true
}

func consume() {
    for {
        msg := <-msgs
        fmt.Println(msg)
    }
}

func main() {
    go produce()
    go consume()
    <-done
}

Is Go good for applications that have to be scalable?

Thank you

Vladislav Mitov

Miracl